home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2678 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.8 KB  |  79 lines

  1. Newsgroups: comp.lang.c,gnu.gcc.help
  2. Path: info.physics.utoronto.ca!olivers
  3. From: olivers@helios.physics.utoronto.ca (Oliver Schonborn)
  4. Subject: Re: [Q] WEIRD macro bug only when compiling with GCC
  5. Message-ID: <DLM9KM.IoJ@info.physics.utoronto.ca>
  6. Nntp-Posting-Host: helios.physics.utoronto.ca
  7. Sender: news@info.physics.utoronto.ca (System Administrator)
  8. Organization: University of Toronto - Dept. of Physics
  9. Date: Tue, 23 Jan 1996 04:24:22 GMT
  10. References: <DLKABL.KGK@info.physics.utoronto.ca> <TANMOY.96Jan21212916@qcd.lanl.gov>
  11.  
  12. In article <TANMOY.96Jan21212916@qcd.lanl.gov>,
  13. Tanmoy Bhattacharya <tanmoy@qcd.lanl.gov> wrote:
  14.  
  15. >In article <DLKABL.KGK@info.physics.utoronto.ca>
  16. >olivers@helios.physics.utoronto.ca (Oliver Schonborn) writes: 
  17. ><snip>
  18. >   The macro *works FINE* when I compile with our HP compiler,
  19. >   cc: it prints out one hundred different numbers.  BUT when
  20. >   I use gcc to compile, instead what it prints is double the
  21. >   result of the first SQR on the line, ie  SQR( i*dx, f) * 2.
  22. >   Why isn't (a=b, b*b)+(a=c, c*c) equal to b*b+c*c for GCC?
  23.  
  24. I meant (a=b, a*a) + (a=c, a*a).
  25.  
  26. >could happen. There is a rule in C which says that the same object
  27. >cannot be modified more than once without intervening sequence
  28. >points. In your code fragment `a' is modified twice without
  29. >intervening sequence point.
  30. >
  31. >The issue is slightly confusing because the comma does enforce a
  32. >sequence point between the previous and the next expressions:
  33. >i.e. there is a sequence point between a=b and b*b; likewise between
  34. >a=c and c*c. However as the + operator does not enforce _any_ order
  35. >(not even the existence of an order) between its two operands, there
  36. >is no sequence point _between_ a=b and a=c.
  37.  
  38. Right about the sequence points. I could see that this would be a
  39. problem if I had something like (a=b) + (a=c).  Then the program sets
  40. a=b, but right after sets a to c, so it would yield 2*c (or 2*b if the
  41. compiler first evaluated the right hand side of the + operator).  *But*
  42. here, the result a*a must be stored in some place independent from a,
  43. say a register or some memory location.  The 2 sides of the + operator
  44. are evaluated independently (ie there is no precedence problem between
  45. the + and = and comma), and the comma *is* a sequence point with forced
  46. order from left to right.  Therefore when the program evaluates (a=b,
  47. a*a) (or the other side of the + operator just revert the conclusion
  48. which follows), if first evaluates b, stores it in a, then does a*a and
  49. stores the result somewhere in memory.  Then it evaluates c, stores it
  50. in a (even if a is now changed, it doesn't change the first a*a, since
  51. that is a stored result somewhere in memory), evaluates a*a, and should
  52. store it somewhere *else* in memory, then adds the contents of those 2
  53. memory locations.  So, there should be no problem.  There is no
  54. ambiguity about say, doing first a=b, then a=c, and only after that
  55. doing a*a and again a*a, because of the parentheses.  Or, why would GCC
  56. store the two results (from each side of the + operator) in the same
  57. memory location, just because a*a occurs on both sides of the + operand?
  58.  
  59. >problem mentioned above, but in addition there is no sequence point
  60. >between the a=c and the first a*a (likewise between the a=b and the
  61. >second a*a). 
  62.  
  63. You must mean something else here, since you just said (and I checked),
  64. that the comma is a sequence point, and not only that, its order is
  65. specified.
  66.  
  67.  
  68. So, having read about sequence points, I still think this expression is
  69. valid and unambiguous (assuming b and c don't contain side effects
  70. themselves).  Where is the error?
  71.  
  72. Thanks,
  73.  
  74. -- 
  75.   _________________________________........----------------,
  76.  /\  OliverS@physics.utoronto.ca                            \
  77.  \_|  Condensed-Matter/Quantum-Optics Theory Group           )
  78.    /  Physics Dept University of Toronto -- Canada          /
  79.